package com.kaka.bus.engine.evaluate.sell;

import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.SwitchCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.ProductInfo;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.util.StatusBarUtil;
import com.kaka.bus.util.U;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;

/**
 * Created by OnionMac on 2018/1/17.
 */

public class SellActivity extends BaseActivity<SellPresenter> implements SellContract.View {

    private static final String ENABLE = "y";
    private static final String UNENABLE = "n";

    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.collapsing_toolbar_layout)
    CollapsingToolbarLayout mCollapsingToolbarLayout;
    @BindView(R.id.app_bar_layout)
    AppBarLayout mAppBarLayout;
    @BindView(R.id.repayplan_swipe)
    SwipeRefreshLayout mRepayplanSwipe;
    @BindView(R.id.sell_coor)
    CoordinatorLayout mSellCoor;
    @BindView(R.id.sell_ll)
    LinearLayout mSellLl;
    @BindView(R.id.selff_coll_ll)
    LinearLayout mSelffCollLl;
    @BindView(R.id.sell_tv_phonemodel)
    TextView mSellTvPhonemodel;
    @BindView(R.id.sell_tv_money)
    TextView mSellTvMoney;
    @BindView(R.id.sell_tv_rentphone)
    TextView mSellTvRentphone;
    @BindView(R.id.sell_switch_rentphone)
    SwitchCompat mSellSwitchRentphone;
    @BindView(R.id.sell_tv_sentmoney)
    TextView mSellTvSentmoney;
    @BindView(R.id.sell_switch_buyphone)
    SwitchCompat mSellSwitchBuyphone;
    @BindView(R.id.sell_commit)
    AppCompatButton mSellCommit;
    @BindView(R.id.sell_tv_buyphone)
    TextView mSellTvBuyphone;
    @BindView(R.id.sell_tv_buymoney)
    TextView mSellTvBuyMoney;
    private List<ProductInfo.CodeValueEntity> mCodeValue;
    private String mAmount;
    private String mProductId;
    private DecimalFormat mDf;

    @Override
    protected void initView() {
        super.initView();
        mDf = new DecimalFormat("0.00");
        mRepayplanSwipe.setProgressViewEndTarget(false, 300);
        mRepayplanSwipe.setColorSchemeColors(getResources().getColor(R.color.toolbar_color));

        mToolbarBack.setOnClickListener(v -> finish());
        mToolbarName.setText("申请出售");
        StatusBarUtil.setTranslucentForImageViewInFragment(this, null);
        TypedValue tv = new TypedValue();
        if (this.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, this.getResources().getDisplayMetrics());
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mSelffCollLl.getLayoutParams();
            int barHeight = U.getBarHeight(this.getApplication());
            params.topMargin = actionBarHeight + barHeight + 10;
            mSelffCollLl.setLayoutParams(params);
        }

        mSellTvPhonemodel.setText(U.connectString("手机型号: ", U.getModel()));
//        buttonEnable(false);
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.loadData(getUserId(), getToken(), Constant.PLATFORM,"4");
    }

    @Override
    public void getProductSuccess(HttpWrapper<ProductInfo> wrapper) {
        closeRefresh();
        if (Constant.SUCCESS_CODE.equals(wrapper.code)) {
            mCodeValue = wrapper.getData().getCodeValue();
            List<ProductInfo.ProductEntity> product = wrapper.getData().getProduct();
            ProductInfo.ProductEntity productEntity = product.get(0);
            mAmount = productEntity.getAmount();
            mProductId = productEntity.getId();
            mSellTvMoney.setText(U.connectString("¥", mDf.format(Double.parseDouble(productEntity.getAmount()))));
            mSellTvRentphone.setText(mCodeValue.get(0).getMeaning());
            mSellTvSentmoney.setText(mCodeValue.get(0).getDescriptions());
            mSellTvBuyphone.setText(mCodeValue.get(1).getMeaning());
            mSellTvBuyMoney.setText(mCodeValue.get(1).getDescriptions());

//            checkButtonState();
        } else {
            showMessage(wrapper.getInfo());
        }
    }

    @Override
    protected void initListener() {
        super.initListener();

        mSellCommit.setOnClickListener(v -> {
//            if (mCodeValue == null) {
//                return;
//            }

            if(TextUtils.isEmpty(mProductId)){
                return;
            }
//
//            String e = mCodeValue.get(0).getEnabledFlag().toLowerCase();
//            String e1 = mCodeValue.get(1).getEnabledFlag().toLowerCase();
//
//            String flag = mSellSwitchRentphone.isChecked() ? ENABLE : UNENABLE;
//            String flag1 = mSellSwitchBuyphone.isChecked() ? ENABLE : UNENABLE;
//
//            if (ENABLE.equals(e) && !flag.equals(e)) {
//                showMessage("请同意租金协议");
//                return;
//            }
//
//            if (ENABLE.equals(e1) && !flag1.equals(e1)) {
//                showMessage("请同意回购协议");
//                return;
//            }
            mPresenter.createProduct(getUserId(), mProductId, mAmount, getToken());
        });

        mRepayplanSwipe.setOnRefreshListener(this::initData);

//        mSellSwitchRentphone.setOnCheckedChangeListener((buttonView, isChecked) -> {
//            checkButtonState();
//        });
//
//        mSellSwitchBuyphone.setOnCheckedChangeListener((buttonView, isChecked) -> {
//            checkButtonState();
//        });
    }

    private void checkButtonState() {
        if (mCodeValue == null) {
            return;
        }
        String flag = mSellSwitchRentphone.isChecked() ? ENABLE : UNENABLE;
        String flag1 = mSellSwitchBuyphone.isChecked() ? ENABLE : UNENABLE;

        String e = mCodeValue.get(0).getEnabledFlag().toLowerCase();
        String e1 = mCodeValue.get(1).getEnabledFlag().toLowerCase();

        if(mSellSwitchRentphone.isChecked()){
            mSellTvSentmoney.setVisibility(View.VISIBLE);
        }else{
            mSellTvSentmoney.setVisibility(View.GONE);
        }

        if(mSellSwitchBuyphone.isChecked()){
            mSellTvBuyMoney.setVisibility(View.VISIBLE);
        }else{
            mSellTvBuyMoney.setVisibility(View.GONE);
        }

        if (ENABLE.equals(e) && !flag.equals(e)) {
            buttonEnable(false);
            return;
        }

        if (ENABLE.equals(e1) && !flag1.equals(e1)) {
            buttonEnable(false);
            return;
        }

        buttonEnable(true);
    }

    @Override
    public void createProduct(HttpWrapper<String> wrapper) {
        if (Constant.SUCCESS_CODE.equals(wrapper.code)) {
            mPresenter.router(this);
        } else {
            showMessage(wrapper.info);
        }
    }

    public void closeRefresh() {
        if (mRepayplanSwipe == null) {
            return;
        }

        if (mRepayplanSwipe.isRefreshing()) {
            mRepayplanSwipe.setRefreshing(false);
        }
    }

    private void buttonEnable(boolean enable){
        mSellCommit.setBackgroundResource(enable?R.drawable.button_ripple_blue:R.drawable.button_gray);
        mSellCommit.setEnabled(enable);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sell;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

}
